// pages/applayRecord/applayRecord.js
let app = getApp()
let request = require('../../utils/request')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    screenHeight: 0,
    pageSize: 20,
    currentPage: 1,
    pageFlag: false,
    applyList: [],
    selectTabIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      screenHeight: wx.getSystemInfoSync().screenHeight
    })
    this.getApplyList()
  },
  selectTab(e) {
    let index = e.currentTarget.dataset.index
    index = parseInt(index)
    if (index == this.data.selectTabIndex) {return}
    this.setData({
      selectTabIndex: index,
      currentPage: 1
    })
    this.getApplyList(false,index + 1)
  },
  /**
   * 获取预约列表
   */
  getApplyList(next,status) {
    status = status ? status : 1
    let that = this
    let parmas = {
      pageNum: this.data.currentPage,
      pageSize: this.data.pageSize,
      status
    }
    request.http('/order-reserve/park-reserve-record/get-reserve-record', 'GET', parmas)
      .then(res => {
        let data = res.data
        let applyList = that.data.applyList
        let pageFlag
        if (data.code == 200) {
          if (next) {
            if (data.data.records.length > 0) {
              applyList = applyList.concat(data.data.records)
              pageFlag = false
            } else {
              pageFlag = true
            }
          } else {
            applyList = data.data.records
          }
          that.setData({
            applyList,
            pageFlag
          })
        }
      })
  },
  /**
   * 取消预约
   */
  cancelApply(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      content: '是否取消车位预约，取消后预约服务费将于7个工作日内退回',
      confirmText: '取消预约',
      cancelText: '再想想',
      success(res) {
        if (res.confirm) {
          request.http('/order-reserve/park-reserve-record/cancel-reserve/' + id, 'POST')
            .then(res => {
              let data = res.data
              if (data.code == 200) {
                wx.showToast({
                  title: '已取消预约',
                  icon: 'none',
                  success() {
                    setTimeout(function() {
                      that.getApplyList()
                    },1000)
                  }
                })
                that.setData({
                  currentPage: 1
                })
              }
            })
        }
      }
    })
  },
  /**
   * 触底加载
   */
  scrollTolower() {
    if (this.data.pageFlag) {
      return
    }
    this.setData({
      pageFlag: true,
      currentPage: this.data.currentPage + 1
    })
    this.getApplyList(true)
  },
})